<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="三级分类" prop="cthrId">
        <el-input
          v-model="queryParams.cthrId"
          placeholder="请输入三级分类"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="品牌" prop="brdId">
        <el-input
          v-model="queryParams.brdId"
          placeholder="请输入品牌"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商品名称" prop="goodsName">
        <el-input
          v-model="queryParams.goodsName"
          placeholder="请输入商品名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商品代号" prop="goodsCode">
        <el-input
          v-model="queryParams.goodsCode"
          placeholder="请输入商品代号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['webshop:GoodsSpuInfo:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['webshop:GoodsSpuInfo:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['webshop:GoodsSpuInfo:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['webshop:GoodsSpuInfo:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="GoodsSpuInfoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="商品主键" align="center" prop="goodsId"/>
      <el-table-column label="一级分类" align="center" prop="coneName"/>
      <el-table-column label="二级分类" align="center" prop="ctwoName"/>
      <el-table-column label="三级分类" align="center" prop="cthrName"/>
      <el-table-column label="品牌" align="center" prop="brandCnName"/>
      <el-table-column label="商品名称" align="center" prop="goodsName"/>
      <el-table-column label="商品代号" align="center" prop="goodsCode"/>
      <el-table-column label="详细说明" align="center" prop="goodsInfo"  :formatter="formatHandler"  />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['webshop:GoodsSpuInfo:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['webshop:GoodsSpuInfo:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改商品对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1000px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">

        <el-form-item label="分类" prop="coneId">
          <el-cascader
            v-model="value"
            :options="options"
            @change="handleChangeCascader"></el-cascader>
        </el-form-item>

        <el-row>
          <el-col :span="8">
            <el-form-item label="一级分类" prop="coneId">
              <el-select v-model.number="form.coneId" placeholder="请选择一级分类">
                <el-option
                  v-for="cone in oneList"
                  :key="cone.coneId"
                  :label="cone.coneName"
                  :value="cone.coneId">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="二级分类" prop="ctwoId">
              <el-select v-model="form.ctwoId" placeholder="请输入二级分类">
                <el-option
                  v-for="ctwo in twoList"
                  :key="ctwo.ctwoId"
                  :label="ctwo.ctwoName"
                  :value="ctwo.ctwoId">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="三级分类" prop="cthrId">
              <el-select v-model="form.cthrId" placeholder="请输入三级分类">
                <el-option
                  v-for="cthr in threeList"
                  :key="cthr.cthrId"
                  :label="cthr.cthrName"
                  :value="cthr.cthrId">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>


        <el-form-item label="品牌" prop="brdId">
          <el-select v-model="form.brdId" placeholder="请输入品牌">
            <el-option
              v-for="brand in brandList"
              :key="brand.brandId"
              :label="brand.brandCnName"
              :value="brand.brandId">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="商品名称" prop="goodsName">
          <el-input v-model="form.goodsName" placeholder="请输入商品名称"/>
        </el-form-item>
        <el-form-item label="商品代号" prop="goodsCode">
          <el-input v-model="form.goodsCode" placeholder="请输入商品代号"/>
        </el-form-item>
        <el-form-item label="详细说明" prop="goodsInfo">
          <el-input v-model="form.goodsInfo" type="textarea" placeholder="请输入内容"/>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listGoodsSpuInfo,
  getGoodsSpuInfo,
  delGoodsSpuInfo,
  addGoodsSpuInfo,
  updateGoodsSpuInfo
} from "@/api/webshop/GoodsSpuInfo";



import {listConeAll, listCascader} from "@/api/webshop/CategoryOne";
import {listCtwoAll} from "@/api/webshop/CategoryTwo";
import {listCthreeAll } from "@/api/webshop/CategoryThree";
import {listBrandAll, getBrandInfoByConeId} from "@/api/webshop/BrandInfo";
import {getDataList} from "@/api/webshop/BaseArea";

export default {
  name: "GoodsSpuInfo",
  data() {
    return {

      value: [],
      options: [],



      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 商品表格数据
      GoodsSpuInfoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        cthrId: null,
        brdId: null,
        goodsName: null,
        goodsCode: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},

      oneList: [],
      twoList: [],
      threeList: [],
      brandList: [],
      brandFullList:[],


    };
  },
  created() {
    this.getList();
    this.getOneList();
    this.getTwoList();
    this.getThreeList();
    this.getBrandList();
    // this.getCascaderList()
    this.getDataList()

  },
  watch:{
    "form.coneId"(val){
      console.log("coneId", val )

      if(val!=null){
        this.brandList = this.brandFullList.filter(brand=>{
          console.log( brand )
          return  brand.coneIds.includes(val);
        })

        this.form.brdId = null

      }else{
        this.brandList = this.brandFullList
        this.form.brdId = null
      }

    }
  },
  methods: {
    getDataList(){
      getDataList().then(resp=>{
        this.options = resp.data;
      })
    },
    getCascaderList(){
      listCascader().then(resp=>{
        this.options = resp.data;
      })
    },
    handleChangeCascader(value) {
      console.log("value-->",value);
      this.form.coneId = value[0]
      this.form.ctwoId = value[1]
      this.form.cthrId = value[2]
    },


    getOneList() {
      listConeAll().then(response => {
        this.oneList = response.data;
      })
    },
    getTwoList() {
      listCtwoAll().then(response => {
        // console.log( response.data )
        this.twoList = response.data;
      })
    },
    getThreeList() {

      listCthreeAll().then(response => {
        this.threeList = response.data;
      })
    },
    getBrandList() {

      listBrandAll().then(response => {
        this.brandFullList = response.data;
      })
    },


    /** 查询商品列表 */
    getList() {
      this.loading = true;
      listGoodsSpuInfo(this.queryParams).then(response => {
        this.GoodsSpuInfoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },

    formatHandler(row, column, cellValue, index){
      return cellValue.length>5?cellValue.substring(0,5)+'...':cellValue
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        goodsId: null,
        coneId: null,
        ctwoId: null,
        cthrId: null,
        brdId: null,
        goodsName: null,
        goodsCode: null,
        goodsInfo: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.goodsId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加商品";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const goodsId = row.goodsId || this.ids
      getGoodsSpuInfo(goodsId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改商品";

        this.value = [ this.form.coneId, this.form.ctwoId, this.form.cthrId ]
        this.form.brdId = response.data.brdId
      });
    },
    /** 提交按钮 */
    submitForm() {
      console.log( this.value )

      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.goodsId != null) {
            updateGoodsSpuInfo(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addGoodsSpuInfo(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const goodsIds = row.goodsId || this.ids;
      this.$modal.confirm('是否确认删除商品编号为"' + goodsIds + '"的数据项？').then(function () {
        return delGoodsSpuInfo(goodsIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('webshop/GoodsSpuInfo/export', {
        ...this.queryParams
      }, `GoodsSpuInfo_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
